#include "VSDLodTerrainNode.h"
#include "VSDLodTerrainGeometry.h"
#include "VSDLodTerrainSwitchNode.h"
#include "VSGraphicInclude.h"
using namespace VSEngine2;
IMPLEMENT_RTTI(VSDLodTerrainNode,VSTerrainNode)
BEGIN_ADD_PROPERTY(VSDLodTerrainNode,VSTerrainNode)
END_ADD_PROPERTY
IMPLEMENT_INITIAL_BEGIN(VSDLodTerrainNode)
IMPLEMENT_INITIAL_END
VSDLodTerrainNode::VSDLodTerrainNode()
{
	m_uiTesselationLevel = MIN_TESSELATION_LEVEL;
	m_uiNumX = MIN_NUM;
	m_uiNumZ = MIN_NUM;

	m_pHeight = NULL;
	m_fHeightScale = 1.0f;
	m_uiExtend = 10;
	m_fScale = 1000.0f;
}

VSDLodTerrainNode::~VSDLodTerrainNode()
{
	VSMAC_DELETEA(m_pHeight);
}

bool VSDLodTerrainNode::CreateChild()
{
	m_pChild.Clear();
	unsigned int uiChildNumX = GetChildNumX();
	unsigned int uiChildNumZ = GetChildNumZ();
	for (unsigned int i = 0 ; i < uiChildNumX ; i++)
	{
		for ( unsigned int j = 0 ; j < uiChildNumZ ; j++)
		{

			VSDLodTerrainSwitchNode * pDTS = NULL;
			pDTS = VS_NEW VSDLodTerrainSwitchNode(i,j);
			if (!pDTS)
			{
				return 0;
			}
			AddChild(pDTS);
			for (unsigned int k = 0 ; k < m_uiTesselationLevel ; k++)
			{
				VSDLodTerrainGeometry * pChild = NULL;
				pChild = VS_NEW VSDLodTerrainGeometry();
				if (!pChild)
				{
					return 0;
				}
				pDTS->AddChild(pChild);
				pChild->CreateMesh(i,j,k,m_uiTesselationLevel);
			}

		}
	}

	return 1;
}

void VSDLodTerrainNode::UpdateNodeAll(double dAppTime)
{
	if (!m_bEnable)
	{
		return;
	}
	VSTerrainNode::UpdateNodeAll(dAppTime);
// 	for (unsigned int i = 0 ; i < m_pChild.GetNum() ; i++)
// 	{
// 		VSSpatial * pChild = m_pChild[i];
// 		((VSTerrainGeometry *)pChild)->ClearInfo();
// 	}
}